package reso.examples.gobackn.gui;

import reso.examples.gobackn.gui.GuiJfreeChartModel.Operation;
import reso.examples.gobackn.transport.App;
import reso.examples.gobackn.transport.DummyPacket;
import reso.examples.gobackn.transport.GoBackNWithCongestionControl;

public class GBNCongContrControler extends GoBackNWithCongestionControl{

	private GuiJfreeChartModel guiJfreeChartModel;
	GBNCongContrControler(int timeOut, double ssthresh, GuiJfreeChartModel guiJfreeChartModel) {
		super(timeOut, ssthresh);
		this.guiJfreeChartModel=guiJfreeChartModel;
		this.guiJfreeChartModel.setSsthreshAndCwnd(Operation.SLOW_START, this.ssthresh, this.cwnd,false);
	}
	
	@Override
	protected void onTimeOut(App app) {
		super.onTimeOut(app);
		CachedInformation cachedInformation = mapCachedByApp.get(app);
		guiJfreeChartModel.setSsthreshAndCwnd(Operation.TIMEOUT, this.ssthresh, this.cwnd,cachedInformation.acksExpected.size()==0);
	}
	
	@Override
	protected void performAck(App appDst, DummyPacket ackMessage)
			throws Exception {
		Integer numberDupAck=this.mapCounterAcks.get(ackMessage);
		super.performAck(appDst, ackMessage);
		
		if(numberDupAck!=null && numberDupAck==2){
			guiJfreeChartModel.setSsthreshAndCwnd(Operation.FAST_RECOVERY, this.ssthresh, this.cwnd,false);
		}else if(this.state==State.SLOW_START){
			guiJfreeChartModel.setSsthreshAndCwnd(Operation.SLOW_START, this.ssthresh, this.cwnd,false);
		}else{
			guiJfreeChartModel.setSsthreshAndCwnd(Operation.CONGESTION_AVOIDANCE, this.ssthresh, this.cwnd,false);
		}
	}
}
